package com.coldchain.admin.config.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.coldchain.common.vo.CarTypeVo;
import com.coldchain.admin.config.entity.CarType;
import com.coldchain.admin.config.mapper.CarTypeMapper;
import com.coldchain.admin.config.service.IAreaService;
import com.coldchain.admin.config.service.ICarTypeService;
import com.coldchain.admin.config.service.IFeeScaleService;
import com.coldchain.common.exception.base.BusinessException;
import com.fangliai.core.base.service.impl.BaseServiceImpl;
import com.fangliai.core.context.BaseContextHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 车型 服务实现类
 * </p>
 *
 * @author fangliai
 * @since 2018-09-13
 */
@Service
public class CarTypeServiceImpl extends BaseServiceImpl<CarTypeMapper, CarType> implements ICarTypeService {

    private static final Logger log= LoggerFactory.getLogger(CarTypeServiceImpl.class);

    @Autowired
    private IAreaService cityService;
    @Autowired
    private IFeeScaleService feeScaleService;

    @Override
    public List<CarTypeVo> getAllCarType() {
        return  baseMapper.getAllCarType();
    }

    @Override
    public CarTypeVo getCarTypeVoInfoById(Integer id) {
        return baseMapper.getCarTypeVoInfoById(id);
    }

    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void addCarType(String params) throws BusinessException {
        try {
            JSONObject object=JSONObject.parseObject(params);
            boolean isMore=object.containsKey("feescal");
            CarType carType=object.getJSONObject("carType").toJavaObject(CarType.class);
            carType.setCreateBy(BaseContextHandler.getUsername());
            carType.setCreateTime(new Date().getTime());
            super.save(carType);
        }catch (Exception e){
            log.error("add carType error:"+e.getMessage());
            throw  new BusinessException("服务器异常");
        }
    }
}
